import {
  handlerGobackClick,
  onlyChangeRouter,
  hint,
  popup,
} from "../../../utils/util";
const app = getApp();
import { Houses } from "../../../models/houses";
const house = new Houses();
import Apipublic from "../../../models/api";
const api = new Apipublic();

Page({
  data: {
    baseImgUrl: getApp().globalImgUrl.config.baseImgUrl,
    activeTab: 0,
    options: {},
    scrollH: "auto",
    isEdit: false,
    detail: {},
    form: {},
    userList: [],
    //加载中状态
    loading: false,
    //没有更多了
    noMore: false,
    //加载失败
    loadingFailed: false,
    //当前已加载页数
    pageNum: 1,
    //---------------------------------
    houseCount: "",
    area: "",
    rentCount: "",
    rentArea: "",
    houseCard: "",
    categoryShow: false,
    natureShow: false,
    useShow: false,
    situationShow: false,
    structureShow: false,
    numberShow: false,
    rentShow: false,
    rentNumShow: false,
    rentUseShow: false,
    modelShow: false,
    categoryList: [],
    natureList: [],
    useList: [],
    situationList: [],
    structureList: [],
    isRentList: [],
    rentUseList: [],
    modelList: [],
  },
  edit() {
    let form = { ...this.data.detail };
    this.setData({
      form,
      houseCount: form.houseCount,
      area: form.area,
      rentCount: form.rentCount,
      rentArea: form.rentArea,
      houseCard: form.houseCard,
      isEdit: true,
    });
  },
  getItem() {
    //房屋类别 house_category
    api.dictItemList("house_category").then((res) => {
      this.setData({
        categoryList: res.result || [],
      });
    });
    // 房屋性质 house_nature
    api.dictItemList("house_nature").then((res) => {
      this.setData({
        natureList: res.result || [],
      });
    });
    // 房屋用途	house_use
    api.dictItemList("house_use").then((res) => {
      this.setData({
        useList: res.result || [],
      });
    });
    // 房屋状态	house_status
    api.dictItemList("house_status").then((res) => {
      this.setData({
        situationList: res.result || [],
      });
    });
    // 房屋结构	house_struct
    api.dictItemList("house_struct").then((res) => {
      this.setData({
        structureList: res.result || [],
      });
    });
    // 是否出租房   isRent
    api.dictItemList("isRent").then((res) => {
      this.setData({
        isRentList: res.result || [],
      });
    });
    // 出租用途	rent_use
    api.dictItemList("rent_use").then((res) => {
      this.setData({
        rentUseList: res.result || [],
      });
    });
    // 房屋户型	house_type
    api.dictItemList("house_type").then((res) => {
      this.setData({
        modelList: res.result || [],
      });
    });
  },
  //打开弹出框
  handleShowPicker(e) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      [name]: true,
    });
  },
  //关闭/取消弹出框
  closePopup(e) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      [name]: false,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDetail(options);
    this.setData({
      options,
    });
  },
  getDetail(options) {
    house.detail(options).then((res) => {
      this.setData({
        detail: res.result || {},
      });
    });
  },
  getUserList(id) {
    house.userlist(this.data.pageNum, 10, id).then((res) => {
      this.setData({
        loading: false,
      });
      var pagesSecond = 0;
      var sizeSecond = 0;
      var totalSecond = 0;
      pagesSecond = res.result.current;
      sizeSecond = res.result.size;
      totalSecond = res.result.total;

      let userList = this.data.userList.concat(res.result.records || []);
      this.setData({
        userList,
      });

      if (pagesSecond * sizeSecond >= totalSecond) {
        this.setData({
          noMore: true,
        });
        return false;
      }
    });
  },
  onChange(e) {
    let index = e.detail.index;
    let id = this.data.detail.id;
    if (index == 1) {
      this.setData({
        pageNum: 1,
        userList: [],
      });
      this.getUserList(id);
    }
  },
  userDetail(e) {
    let memberId = e.currentTarget.dataset.id;
    let houseId = this.data.detail.id;
    wx.navigateTo({
      url: "../userDetail/index?memberId=" + memberId + "&houseId=" + houseId,
    });
  },
  myDelete(e) {
    let id = e.currentTarget.dataset.id;
    popup(() => {
      house.detele(id).then((res) => {
        let houseId = this.data.detail.id;
        this.setData({
          pageNum: 1,
          userList: [],
        });
        this.getUserList(houseId);
      });
    });
    return;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var query = wx.createSelectorQuery();
    query
      .select("#scrollH")
      .boundingClientRect((res) => {
        let height = res.height - 130;
        // let height = res.height - 44;
        this.setData({
          scrollH: height,
        });
      })
      .exec();
  },
  handlerGobackClick,
  onlyChangeRouter,
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getItem();
  },
  scrollBottom() { },
  scrollBottom1() {
    if (!this.data.loading && !this.data.noMore) {
      this.setData({
        loading: true,
        pageNum: this.data.pageNum + 1,
      });
      this.getUserList(this.data.detail.id);
    }
  },
  //----------------------------------------------
  showConfirm6(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        houseCategory: value.value,
        houseCategoryDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm7(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        houseNature: value.value,
        houseNatureDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm8(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        houseUse: value.value,
        houseUseDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm9(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        houseStatus: value.value,
        houseStatusDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm10(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        houseStruct: value.value,
        houseStructDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm11(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        isRent: value.value,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm12(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        rentUse: value.value,
        rentUseDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm13(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        houseType: value.value,
        houseTypeDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  save() {
    let form = this.data.form;
    let houseCount = this.data.houseCount;
    let area = this.data.area;
    let rentCount = this.data.rentCount;
    let rentArea = this.data.rentArea;
    let houseCard = this.data.houseCard;

    //正整数
    let pattern1 = /^[1-9]\d*$/;
    //正数
    let pattern2 = /^[1-9]\d*.*\d*$/;
    if (!form.houseCategoryDesc) {
      hint("请选择房屋类别");
      return;
    }
    if (!form.houseNatureDesc) {
      hint("请选择房屋性质");
      return;
    }
    if (!form.houseUseDesc) {
      hint("请选择房屋用途");
      return;
    }
    if (!form.houseStatusDesc) {
      hint("请选择房屋现状");
      return;
    }
    if (!form.houseStructDesc) {
      hint("请选择房屋结构");
      return;
    }
    if (!houseCount || !pattern1.test(houseCount)) {
      hint("请输入正确房屋间数");
      return;
    }
    if (!area || !pattern2.test(area)) {
      hint("请输入正确房屋面积");
      return;
    }
    // if (!form.isRentDesc) {
    //   hint("请选择是否出租房");
    //   return;
    // }
    if (!form.isRent) {
      hint("请选择是否出租房");
      return;
    }
    if (form.isRent == "是") {
      if (
        !rentCount ||
        Number(rentCount) > Number(houseCount) ||
        !pattern1.test(rentCount)
      ) {
        hint("请输入正确出租间数");
        return;
      }
      if (
        !rentArea ||
        Number(rentArea) > Number(area) ||
        !pattern2.test(rentArea)
      ) {
        hint("请输入正确出租面积");
        return;
      }
      // if (!form.rentUseDesc) {
      //   hint("请选择出租用途");
      //   return;
      // }
      if (!form.rentUse) {
        hint("请选择出租用途");
        return;
      }
    } else {
      rentCount = 0;
      rentArea = 0;
      form.rentUse = "";
    }
    if (!form.houseTypeDesc) {
      hint("请选择房屋户型");
      return;
    }
    form = {
      ...form,
      houseCount,
      area,
      rentCount,
      rentArea,
      houseCard,
      kinship: 0,
      houseId: form.id,
      commId: form.commid,
    };
    house.add(form).then((res) => {
      hint(res.message);
      this.setData({
        isEdit: false,
      });
      this.getDetail(this.data.options);
    });
  },
});
